package org.robot.work;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.robot.Spider;
import org.robot.component.Page;
import org.robot.utils.ExcelUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class Chinakjh extends Spider {
	String[] maturityArr = {"", "小试", "中试", "工业化实验", "成熟"};
	String[] coopwayArr = {"", "整体转让（技术买断）", "技术许可", "合作开发", "技术入股（股权合作）", "其他"};
	String[] typeArr = {"", "项目投资需求", "技术难题需求"};
	String[] timeLimitArr = {"", "半年", "一年", "一年以上"};
	List<String> tagList = new ArrayList<>();
	List<String> demandTagList = new ArrayList<>();
	List<String> unitTagList = new ArrayList<>();
	
	public Chinakjh() {
		tagList.add("项目名称");
		tagList.add("所属领域");
		tagList.add("成果单位");
		tagList.add("成果简介");
		tagList.add("技术成熟度");
		tagList.add("合作方式");
		tagList.add("发布时间");
		
		demandTagList.add("需求名称");
		demandTagList.add("需求单位");
		demandTagList.add("需求简介");
		demandTagList.add("需求类型");
		demandTagList.add("需求发布时间");
		demandTagList.add("所属领域");
		demandTagList.add("解决时限");
		demandTagList.add("合作方式");
		
		unitTagList.add("高校名称");
		unitTagList.add("所在地区");
		unitTagList.add("联系人");
		unitTagList.add("邮箱");
		unitTagList.add("地址");
	}

	@Override
	public void parse(Page page) throws Exception {
		String text = page.text();
		JSONObject jsonObj = JSONObject.parseObject(text);
		JSONArray jsonArray = jsonObj.getJSONArray("rows");
		if (page.url().equals("https://www.chinakjh.com/api/v1/kjh_achi/list?page=1&rows=2120&domain=1111")) {
			List<List<String>> contentLists = IntStream.range(0, jsonArray.size())
					.mapToObj(i -> parseJo(jsonArray.getJSONObject(i))).collect(Collectors.toList());
			ExcelUtil.writeExcel(tagList, contentLists, "D:/webSpider/Chinakjh/科技数据.xls", "科技成果");
			
		} else if (page.url().equals("https://www.chinakjh.com/api/v1/kjh_demand/list?page=1&rows=304&domain=1111")) {
			List<List<String>> contentLists = IntStream.range(0, jsonArray.size())
					.mapToObj(i -> parseDemand(jsonArray.getJSONObject(i))).collect(Collectors.toList());
			ExcelUtil.writeExcel(demandTagList, contentLists, "D:/webSpider/Chinakjh/科技数据.xls", "技术需求");
			
		} else if (page.url().equals("https://www.chinakjh.com/api/v1/kjh_unit/list?page=1&rows=427&type=1")) {
			List<List<String>> contentLists = IntStream.range(0, jsonArray.size())
					.mapToObj(i -> parseUnit(jsonArray.getJSONObject(i))).collect(Collectors.toList());
			ExcelUtil.writeExcel(unitTagList, contentLists, "D:/webSpider/Chinakjh/科技数据.xls", "会员单位");
		}
	}

	private List<String> parseUnit(JSONObject jsonObject) {
		String name = jsonObject.getString("name");
		String province = jsonObject.getString("province_name");
		String contact = jsonObject.getString("contact");
		String email = jsonObject.getString("email");
		String address = province+jsonObject.getString("city_name")+jsonObject.getString("county_name")+jsonObject.getString("stree");
		List<String> list = new ArrayList<>();
		list.add(name);
		list.add(province);
		list.add(contact);
		list.add(email);
		list.add(address);
		return list;
	}

	private List<String> parseDemand(JSONObject jsonObject) {
		String name = jsonObject.getString("name");
		String unit = jsonObject.getString("unit_name");
		String summary = jsonObject.getString("summary");
		summary = summary.replaceAll("<.+?>", "").replaceAll("\\s+", "").replace("\u0026nbsp;", "");
		int type = jsonObject.getIntValue("type");
		String milli = jsonObject.getString("create_time").substring(0, 13);
		String date = toDate(Long.parseLong(milli));
		String field = jsonObject.getString("domain_name");
		int timeLimit = jsonObject.getIntValue("time_limit");
		String[] coopArr = jsonObject.getString("coop_way").split(",");
		String coopWay = Arrays.stream(coopArr).mapToInt(Integer::parseInt).mapToObj(coopInt -> coopwayArr[coopInt])
				.collect(Collectors.joining(","));
		List<String> list = new ArrayList<>();
		list.add(name);
		list.add(unit);
		list.add(summary);
		list.add(typeArr[type]);
		list.add(date);
		list.add(field);
		list.add(timeLimitArr[timeLimit]);
		list.add(coopWay);
		return list;
	}

	private List<String> parseJo(JSONObject jsonObject) {
		String name = jsonObject.getString("name");
		String field = jsonObject.getString("domain_name");
		String unit = jsonObject.getString("unit_name");
		String summary = jsonObject.getString("summary");
		summary = summary.replaceAll("<.+?>", "").replaceAll("\\s+", "").replace("\u0026nbsp;", "");
		int maturity = jsonObject.getIntValue("maturity");//成熟度
		String[] coopArr = jsonObject.getString("coop_way").split(",");
		String coopWay = Arrays.stream(coopArr).mapToInt(Integer::parseInt).mapToObj(coopInt -> coopwayArr[coopInt])
			.collect(Collectors.joining(","));
		String milli = jsonObject.getString("create_time").substring(0, 13);
		String date = toDate(Long.parseLong(milli));
		List<String> list = new ArrayList<>();
		list.add(name);
		list.add(field);
		list.add(unit);
		list.add(summary);
		list.add(maturityArr[maturity]);
		list.add(coopWay);
		list.add(date);
		return list;
	}

	private String toDate(long milli) {
		Instant instant = Instant.ofEpochMilli(milli);
		LocalDate date = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate();
		String d = date.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
		return d;
	}

	public static void main(String[] args) throws Exception {
		Chinakjh c = new Chinakjh();
		c.setBaseDirectory("D:\\webSpider\\Chinakjh");
		c.addSeed("https://www.chinakjh.com/api/v1/kjh_achi/list?page=1&rows=2120&domain=1111");
		//技术需求
		c.addSeed("https://www.chinakjh.com/api/v1/kjh_demand/list?page=1&rows=304&domain=1111");
		//会员单位
		c.addSeed("https://www.chinakjh.com/api/v1/kjh_unit/list?page=1&rows=427&type=1");
		c.start();
		
	}
	

}
